/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgAddStock.java
 *
 * Created on Aug 18, 2012, 4:29:52 PM
 */

package Model.dialog;

import DataBase.DBManagerment;
import Processing.dbclass.Stock;
import java.sql.Connection;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class dlgAddStock extends javax.swing.JDialog {

    /** Creates new form dlgAddStock */
    public dlgAddStock(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * Đối tượng của class DBManagerment
     */
    private DBManagerment db = new DBManagerment();

    /**
     * Một Connection
     */
    private Connection con = db.ConnectServer();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel40 = new javax.swing.JLabel();
        jLabel41 = new javax.swing.JLabel();
        txtStockAddCost = new javax.swing.JTextField();
        txtStockAddName = new javax.swing.JTextField();
        txtStockAddID = new javax.swing.JTextField();
        btnStockAction = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        txtStockAddItemAvail = new javax.swing.JTextField();
        jLabel42 = new javax.swing.JLabel();
        jLabel39 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Stock");
        setResizable(false);

        jLabel40.setText("Stock's Name:");

        jLabel41.setText("Cost:");

        btnStockAction.setText("Add");
        btnStockAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStockActionActionPerformed(evt);
            }
        });

        jButton7.setText("Cancel");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jLabel42.setText("Items Available:");

        jLabel39.setText("Stock's ID:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel39)
                    .addComponent(jLabel41)
                    .addComponent(jLabel40)
                    .addComponent(jLabel42))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtStockAddID, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtStockAddItemAvail, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtStockAddCost, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE))
                    .addComponent(txtStockAddName, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(52, Short.MAX_VALUE)
                .addComponent(btnStockAction, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton7)
                .addGap(45, 45, 45))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel39)
                    .addComponent(txtStockAddID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel40)
                    .addComponent(txtStockAddName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel41)
                    .addComponent(txtStockAddCost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel42)
                    .addComponent(txtStockAddItemAvail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnStockAction)
                    .addComponent(jButton7))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnStockActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStockActionActionPerformed
        // TODO add your handling code here:
        if (txtStockAddCost.getText().length() != 0) {
            try {
                double cost = Double.parseDouble(txtStockAddCost.getText());

                if (cost == 0) {
                    return;

                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "Cost input wrong!");
                return;
            }
        }

        if (txtStockAddItemAvail.getText().length() != 0) {
            try {
                int it_av = Integer.parseInt(txtStockAddItemAvail.getText());

                if (it_av == 0) {
                    return;

                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "Items available input wrong!");
                return;
            }
        }

        if (txtStockAddID.getText().length() != 3) {
            //Kiển tra nếu nhập vào ko đúng 6 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Stock's ID must have exactly three character!");
        }else if(db.searchTest(con, 1, txtStockAddID.getText()).getRowCount() != 0){
            // Kiểm tra xem có Patient này ko, dùng method search Patient theo PatientID
            // trả về PikaTableModel, rồi xem PikatableModel này có số row == 0 hay ko?
            // Nếu số Row == 0 tức là ko có Patient với PatientID này

            JOptionPane.showMessageDialog(this, "This Stock's ID existed!");
        }else if (txtStockAddName.getText().length() == 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Stock's name must have!");
        } else if (txtStockAddCost.getText().length() == 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Cost must have!");
        }else if (Double.parseDouble(txtStockAddCost.getText()) < 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Cost must great than 0!");
        } else if (txtStockAddItemAvail.getText().length() == 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Items available must have!");
        }else if (Integer.parseInt(txtStockAddItemAvail.getText()) < 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Items available must great than 0!");
        }else{
            // Trong add bill này CollectorID có thể để trống nên ko cần kiểm tra

            // Nếu toàn bộ quá trình kiểm tra ko sai thì bắt đầu add vào

            // Tạo mới một Object Bill để chứa thông tin
            sto = new Stock();

            sto.setStockID(txtStockAddID.getText());

            sto.setStockName(txtStockAddName.getText());

            sto.setCost(Double.parseDouble(txtStockAddCost.getText()));

            sto.setInum(Integer.parseInt(txtStockAddItemAvail.getText()));

            sto.setDnum(0);

            this.setVisible(false);

            // DateMade và DatePaid ko phải làm vì tớ đã sửa lại method add trong
            // DBManager, sẽ lấy ngày hiện tại
            // Report có DateMade tương tự cũng được tớ sủa method rồi
        }
    }//GEN-LAST:event_btnStockActionActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton7ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgAddStock dialog = new dlgAddStock(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    /**
     * Biến chứa dữ liệu của Test
     */
    private Stock sto;

    /**
     * Method trả về bill để chương trình xử lý
     * @return Test
     */
    public Stock getStock() {
        return sto;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnStockAction;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel39;
    private javax.swing.JLabel jLabel40;
    private javax.swing.JLabel jLabel41;
    private javax.swing.JLabel jLabel42;
    private javax.swing.JTextField txtStockAddCost;
    private javax.swing.JTextField txtStockAddID;
    private javax.swing.JTextField txtStockAddItemAvail;
    private javax.swing.JTextField txtStockAddName;
    // End of variables declaration//GEN-END:variables

}
